import { ref } from 'vue';
import { defineStore } from 'pinia';
import i18n from '@/locales';

export const useLangStore = defineStore(
  'lang',
  () => {
    const lang = ref('zhCn');
    // 设置系统语言
    const setLang = (locale: 'zhCn' | 'en') => {
      lang.value = locale;
      localStorage.setItem('lang', locale);
      i18n.global.locale.value = locale;
    };
    // 获取当前系统语言
    const getLang = () => {
      lang.value = localStorage.getItem('lang') || 'zhCn';
      return lang.value;
    };
    return { setLang, getLang };
  },
  {
    // 持久化处理
    persist: true
  }
);
